package com.example.myapplication

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.google.android.material.appbar.CollapsingToolbarLayout


class MainActivity : AppCompatActivity() {
    val tag = this.javaClass.simpleName

    @SuppressLint("CheckResult")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val fruit = Fruit("苹果香蕉火龙果！", R.mipmap.meizi)
        val toolbar: Toolbar = findViewById<View>(R.id.toolbar) as Toolbar
        val collapsingToolbar =
            findViewById<View>(R.id.collsping_Toolbar) as CollapsingToolbarLayout
        val fruitImageView: ImageView =
            findViewById<View>(R.id.fruit_image_view) as ImageView
        val fruitContentText =
            findViewById<View>(R.id.fruit_content_text) as TextView
        val actionBar: ActionBar? = supportActionBar
        actionBar?.hide()
        actionBar?.setDisplayHomeAsUpEnabled(true)

        val fruitContent = generatorFruitContent(fruit.name)
        fruitContentText.text = fruitContent
//
//        Single.just("111")
//            .subscribe { t1, t2 ->
//                Log.e(tag, "Single t1:$t1 -thread:${Thread.currentThread().name}")
//            }
//
//        Single.just("222")
//            .subscribe { t1 ->
//                Log.e(tag, "Single t1:$t1 -thread:${Thread.currentThread().name}")
//            }
//
//        Observable.just("111", "22", "3")
//            .subscribe(
//                {
//                    Log.e(tag, "Observable length:$it -thread:${Thread.currentThread().name}")
//                },
//                {
//                    Log.e(tag, "Observable length:$it -thread:${Thread.currentThread().name}")
//                }, {
//                    Log.e(tag, "Observable completes")
//                }
//            )


//        Observable.just("111", "22", "3")
//            .zipWith(
//
//                ObservableSource<String> {
//
//                },
//
//                BiFunction<String, String, ArrayList<String>> { t1, t2 ->
//                    Log.e(tag, "t1:$t1 -thread:${Thread.currentThread().name}")
//                    Log.e(tag, "t2:$t2 -thread:${Thread.currentThread().name}")
//
//                    val list = ArrayList<String>()
//                    list.add(t1)
//                    list.add(t2)
//                    return@BiFunction list
//                }
//            )
//            .subscribeOn(Schedulers.io())
//
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe {
//                it.forEach {
//                    Log.e(tag, "length:$it -thread:${Thread.currentThread().name}")
//
//                }
//            }

//            .subscribe {
//                Log.e(tag, "length:$it -thread:${Thread.currentThread().name}")
//
//            }

//        val str: String? = null
//        Observable.create(ObservableOnSubscribe<String> {
//            Log.e(tag, "it:$it -thread:${Thread.currentThread().name}")
//
//            if (str.isNullOrEmpty()) {
//                it.onNext(null.toString())
//            } else {
//                it.onNext(str)
//            }
//        })
//            .subscribeOn(Schedulers.io())//前面是IO线程
//            .observeOn(AndroidSchedulers.mainThread())//后面是主线程
//            .subscribe(
//                {
//                    Log.e(tag, "subscribe it:$it -thread:${Thread.currentThread().name}")
//
//                }, {
//
//                })

//        val image1 = getImage1()
//        val image2 = getImage2()
//        Observable.zip(image1, image2, BiFunction<String, String, String> { t1, t2 ->
//
//            return@BiFunction t1 + t2
//        }).doOnNext {
//            Log.e(tag, "Observable zip doOnNext:$it -thread:${Thread.currentThread().name}")
//        }.doOnComplete(Action {
//            Log.e(tag, "Observable zip doOnComplete")
//
//        }).subscribe {
//            Log.e(tag, "Observable zip subscribe:$it -thread:${Thread.currentThread().name}")
//        }
//
//
//        val image3 = getImage3()
//        val image4 = getImage4()
//        Single.zip(image3, image4, BiFunction<String, String, String> { t1, t2 ->
//
//            return@BiFunction t1 + t2
//        }).subscribeOn(
//            Schedulers.io()
//        ).doOnSuccess(Consumer {
//                Log.e(tag, "Single zip doOnSuccess:$it -thread:${Thread.currentThread().name}")
//
//            }).subscribe()


    }
    override fun onStop() {
        super.onStop()
        Log.e("-----打开---onStop---","")

    }
    //填充卡片布局的text数据
    private fun generatorFruitContent(fruitName: String): String? {
        val fruitContent = StringBuilder()
        for (i in 0..499) {
            fruitContent.append(fruitName)
        }
        return fruitContent.toString()
    }

    fun onclick(view: View) {
        startActivity(Intent(this,FirstAct::class.java))
    }


//    fun login(
//
//    ): Single<String>? {
//        return null
//
//    }

//    fun getImage1(): Observable<String> {
//        return Observable.create {
//            it.onNext("image1 url")
//            it.onComplete()
//        }
//    }
//
//    fun getImage2(): Observable<String> {
//        return Observable.create {
//            it.onNext("image2 url")
//            it.onComplete()
//        }
//    }
//
//    fun getImage3(): Single<String> = Single.create { it.onSuccess("getImage3") }
//    fun getImage4(): Single<String> = Single.create { it.onSuccess("getImage4") }

}